let data = {};
let editingId = null;
let requestUrl = null;

function addHandler() {
	requestUrl = "/qiling_taozhuang/insert";

	$("#taozhuang_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#taozhuang_list").datagrid("getSelected");

	if(rowData) {
		requestUrl = "/qiling_taozhuang/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#note").textbox("setValue", rowData.note);
		$("#type").combobox("setValue", rowData.type);
		$("#count").numberspinner("setValue", rowData.count);
		$("#template").textbox("setValue", rowData.template);

		$("#taozhuang_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function imageHandler() {
	let rowData = $("#taozhuang_list").datagrid("getSelected");

	if(rowData) {
		$("#taozhuang_id").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function effectHandler() {
	let rowData = $("#taozhuang_list").datagrid("getSelected");

	if(rowData) {
		$("#effect_list").datagrid("reload", {
			taozhuangId: rowData.id
		});

		$("#effect_dialog").dialog("open");
	} else {
		alertMsg("请选择要一条记录！", "warning");
	}
}

$(document).ready(function() {

	$("#_type_").combobox({
		url: "/qiling_taozhuang_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-套装类型-"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let type = $("#_type_").combobox("getValue");

		$("#taozhuang_list").datagrid("load", {
			type: type
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});
	
	/******************************************************************************************************/

	$("#name").textbox({
        width: 120,
		required: true
	});
	
	$("#type").combobox({
		url: "/qiling_taozhuang_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true,
		panelHeight: "auto"
	});

	$("#count").numberspinner({
		min: 2,
		max: 6,
		width: 120,
		editable: true,
		required: true
	});
	
	$("#note").textbox({
		width: 320,
    	height: 100,
    	multiline:true, 
    	required:true
	});

	$("#template").textbox({
		width: 320,
    	height: 100,
    	multiline:true,
    	required:true
	});

	$("#taozhuang_dialog").dialog({
		title: "器灵套装",
		width: 440,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#taozhuang_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#taozhuang_dialog").dialog("close");
						$("#taozhuang_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#taozhuang_dialog").dialog("close");
				$("#taozhuang_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#taozhuang_list", "/qiling_taozhuang/upload");

	fetchUploadDialog("/qiling_taozhuang/updateById", function () {
		$("#taozhuang_list").datagrid("reload");
	});

	/******************************************************************************************************/

	let pageList = [5, 10, 20, 40];

	$("#effect_list").datagrid({
		url: "/qiling_taozhuang_effect/selectByPage",
		method: "get",
		width: 500,
		height: 300,
		pagination: true,
		pageList: pageList,
		pageSize: pageList[0],
		/**
		 * 结束行内编辑事件
		 * @param rowIndex 行号
		 * @param rowData 行数据
		 * @param changes 变更的行数据
		 */
		onAfterEdit: function (rowIndex, rowData, changes) {
			data = {
				id: rowData.id,
				taozhuangId: rowData.taozhuangId,
				count: changes.count ? changes.count : rowData.count,
				propertyId: changes.propertyId ? changes.propertyId : rowData.propertyId,
				propertyValue: changes.propertyValue ? changes.propertyValue : rowData.propertyValue
			};
		},
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				let rowData = $("#taozhuang_list").datagrid("getSelected");

				if(rowData) {
					let datagrid = $("#effect_list");

					datagrid.datagrid("appendRow",{
						id: null,
						count: 0,
						propertyId: null,
						propertyValue: 0,
						taozhuangId: rowData.id
					});

					// 得到表格数据总行数
					let total = datagrid.datagrid("getRows").length;

					editingId = total - 1;
					requestUrl = "/qiling_taozhuang_effect/insert";

					datagrid.datagrid("beginEdit", editingId);
				} else {
					unselected();
				}
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				let datagrid = $("#effect_list");

				if (editingId != null) {
					confirmAction("上一次修改的数据未保存，是否现在保存？", function () {
						datagrid.datagrid("endEdit", editingId);
					}, function () {
						datagrid.datagrid("selectRow", editingId);
					});
				} else {
					let row = datagrid.datagrid("getSelected");

					if (row) {
						// 获取行索引，这个索引从0开始
						let rowIndex = datagrid.datagrid("getRowIndex", row);

						editingId = rowIndex;
						requestUrl = "/qiling_taozhuang_effect/updateById";

						datagrid.datagrid("beginEdit", rowIndex);
					} else {
						unselected();
					}
				}
			}
		}, "-", {
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				if (editingId != null) {
					// 只有结束编辑才能获取到最新的值
					$("#effect_list").datagrid("endEdit", editingId);

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$("#effect_list").datagrid("reload");
					}, error);

					editingId = null;
				}
			}
		}, "-", {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				if (editingId != null) {
					$("#effect_list").datagrid("cancelEdit", editingId);

					editingId = null;
				}
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				let rowData = $("#effect_list").datagrid("getSelected");

				if(rowData) {
					$.messager.confirm("提示", "是否确认删除？", function(bool) {
						if (bool) {
							ajaxPost("/qiling_taozhuang_effect/deleteById", {
								id: rowData.id
							}, function(resp) {
								showMsg(resp.message);

								$("#effect_list").datagrid("reload");
							}, error);
						}
					});
				} else {
					unselected();
				}
			}
		}],
		columns: [[
			{field: "count", title: "件数", align: "center", width: 100
				, editor: {
					type: "numberspinner",
					options: {
						min: 2,
						max: 6
					}
				}
				, formatter: function(value) {
					return "<div class='blue'>" + value + "件</div>";
				}
			},
			{field: "propertyId", title: "增加属性", align: "center", width: 100
				, editor: {
					type: "combobox",
					options: {
						url: "/property/selectAll",
						valueField: "id",
						textField: "name",
						panelHeight: "auto"
					}
				}
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/property/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#property_" + rowIndex).html(data.name);
						}, error);

						return "<div class='blue' id='property_" + rowIndex + "'></div>";
					}

					return "/";
				}
			},
			{field: "propertyValue", title: "数值", align: "center", width: 100, editor: "numberspinner"
			},
			{field: "taozhuangId", title: "器灵套装", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/qiling_taozhuang/selectById", {
						id: value
					}, function (resp) {
						let data = resp.data;

						$("#taozhuang_" + rowIndex).attr("src", data.image)
							.attr("title", data.name);
					}, error);

					return "<img height='32' id='taozhuang_" + rowIndex + "' />";
				}
			}
		]]
	});

	$("#effect_dialog").dialog({
		title: "器灵套装效果"
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#taozhuang_list").datagrid({
		url: "/qiling_taozhuang/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-shield",
			text: "效果",
			handler: function() {
				effectHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "type", title: "套装类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/qiling_taozhuang_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#type_" + rowIndex).html(data.name);
					}, error);

					return "<div id='type_" + rowIndex + "'></div>";
				}
			},
			{field: "name", title: "套装名称", align: "center", width: 100},
			{field: "image", title: "图片", align: "center", width: 100
				, formatter: function(value) {
					return "<img height='" + size + "' src='" + value + "' />";
				}
			},
			{field: "count", title: "件数", align: "center", width: 100},
			{field: "note", title: "说明", align: "center", width: 500}
		]]
	});

});